package com.pzhu.dormitory.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pzhu.dormitory.entity.StudentEntity;
import com.pzhu.dormitory.service.StudentService;
import com.pzhu.dormitory.service.UserService;
import com.pzhu.dormitory.vo.CostWVo;
import com.pzhu.dormitory.vo.UserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;

import java.util.Map;
import java.util.Optional;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pzhu.dormitory.utils.PageUtils;
import com.pzhu.dormitory.utils.Query;

import com.pzhu.dormitory.dao.CostwDao;
import com.pzhu.dormitory.entity.CostwEntity;
import com.pzhu.dormitory.service.CostwService;


@Service("costwService")
public class CostwServiceImpl extends ServiceImpl<CostwDao, CostwEntity> implements CostwService {

    @Autowired
    private CostwDao costwDao;

    @Autowired
    private UserService userService;
    @Autowired
    private StudentService studentService;

    @Override
    public PageUtils<CostwEntity> queryPage(Map<String, Object> params) {
        IPage<CostwEntity> page = this.page(
                new Query<CostwEntity>().getPage(params),
                new QueryWrapper<>()
        );
        return new PageUtils<>(page);
    }

    @Override
    public PageUtils<CostWVo> queryPageCostVo(Map<String, Object> params) {
        Page<CostWVo> page = new Query<CostWVo>().getPage(params);
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        UserVo userVo = userService.getUserInfoByUsername(authentication.getName());
        Optional<StudentEntity> one = studentService.lambdaQuery().eq(StudentEntity::getSno, userVo.getUsername()).oneOpt();
        one.ifPresent(studentEntity -> {
            params.put("studentId",one.get().getId());
        });
        // 查询基本数据
        Page<CostWVo> roomVoPage = costwDao.queryPageVo(page, params);
        return new PageUtils<>(roomVoPage);
    }

}